package com.ruoyi.web.controller.question;

import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.question.domain.QDoorRank;
import com.ruoyi.question.service.IQDoorRankService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.question.domain.QZoom;
import com.ruoyi.question.service.IQZoomService;
import com.ruoyi.common.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 房间Controller
 *
 * @author zouhao
 * @date 2024-09-11
 */
@RestController
@RequestMapping("/system/zoom")
public class QZoomController extends BaseController<QZoom>
{
    @Autowired
    private IQZoomService qZoomService;
    @Autowired private IQDoorRankService doorRankService;
    @Override
    protected IService<QZoom> getService() {
        return qZoomService;
    }
    /**
     * 查询房间列表
     */
    @PreAuthorize("@ss.hasPermi('system:zoom:list')")
    @GetMapping("/list")
    public TableDataInfo list(Integer pageNum,Integer pageSize,QZoom qZoom)
    {
        return super.list(pageNum,pageSize,qZoom);
    }

    /**
     * 导出房间列表
     */
    @PreAuthorize("@ss.hasPermi('system:zoom:export')")
    @Log(title = "房间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QZoom qZoom)
    {
        super.exportExcel(response,qZoom,"房间数据");
    }

    /**
     * 获取房间详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:zoom:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return super.getInfo(id);
    }

    /**
     * 新增房间
     */
    @PreAuthorize("@ss.hasPermi('system:zoom:add')")
    @Log(title = "房间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add()
    {
        while(true){
            String zoomCode=RandomUtil.randomNumbers(4);
            QZoom _zoom=qZoomService.lambdaQuery().eq(QZoom::getCode,zoomCode).eq(QZoom::getIsOver,true).one();
            if(_zoom==null){
                QZoom zoom=new QZoom();
                zoom.setCode(RandomUtil.randomNumbers(4));
                zoom.setIsOver(false);
                zoom.setCreateTime(new Date());
                zoom.setUpdateTime(new Date());
                return super.add(zoom);
            }
        }
    }

    /**
     * 修改房间
     */
    @PreAuthorize("@ss.hasPermi('system:zoom:edit')")
    @Log(title = "房间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QZoom qZoom)
    {
        return super.edit(qZoom);
    }

    /**
     * 删除房间
     */
    @PreAuthorize("@ss.hasPermi('system:zoom:remove')")
    @Log(title = "房间", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return super.remove(ids);
    }

    @GetMapping("/result")
    public AjaxResult result(@RequestParam Integer zoomId){
        List<QDoorRank> list=doorRankService.lambdaQuery().
                eq(QDoorRank::getZoomId,zoomId).orderByAsc(QDoorRank::getRank).list();
        return AjaxResult.success(list);
    }
}
